package com.chocus.common.utils;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestUtils {

	public static String getCSESSIONID(HttpServletRequest request, HttpServletResponse response) {
		// 获取cookie
		Cookie[] cookies = request.getCookies();
		if (cookies != null && cookies.length > 0) {
			for (Cookie cookie : cookies) {
				// 从cookie中获取CSESSIONID
				if ("CSESSIONID".equals(cookie.getName())) {
					// 找到则返回
					return cookie.getValue();
				}
			}
		}
		// 如果没有查到，则创建并返回
		String cSessionId = UUID.randomUUID().toString().replaceAll("-", "");
		Cookie cookie = new Cookie("CSESSIONID", cSessionId);
		cookie.setPath("/");
		cookie.setMaxAge(-1);
		response.addCookie(cookie);
		return cSessionId;
	}
}
